package cn.edu.scau.cmi.front.dao;

import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import cn.edu.scau.cmi.ema.dao.AbstractJpaDao;
import cn.edu.scau.cmi.ema.dao.PictureDAO;
import cn.edu.scau.cmi.ema.domain.Picture;
import cn.edu.scau.cmi.ema.service.PictureService;
import cn.edu.scau.cmi.ema.service.PictureServiceImpl;

@Repository("CmiPictureDAO")
@Transactional
public class CmiPictureDAOImpl extends AbstractJpaDao implements CmiPictureDAO {

	@PersistenceContext(unitName = "ema")
	private EntityManager entityManager;

	@Autowired
	PictureDAO pictureDAO;
	
	@Autowired
	PictureService pictureService;
	
	@SuppressWarnings("unchecked")
	@Override
	@Transactional 
	public Set<Picture> findPicturesByIdDesc(Integer startResult, Integer maxRows) throws DataAccessException {
		Query query = createNamedQuery("findPicturesByIdDesc", startResult, maxRows);
		return new LinkedHashSet<Picture>(query.getResultList());
	}

//	@Override
	public EntityManager getEntityManager() {
		// TODO Auto-generated method stub
		return entityManager;
	}

	@Override
	public boolean canBeMerged(Object o) {
		// TODO Auto-generated method stub
		return false;
	}



	@Override
	public Picture executeQueryByNameSingleResult(String queryName) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Picture executeQueryByNameSingleResult(String queryName, Object... parameters) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Picture executeQuerySingleResult(String queryString, Object... parameters) {
		// TODO Auto-generated method stub
		return null;
	}


	@Override
	public Set getTypes() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public void updatePicture() {
		Set<Picture> pictures = pictureDAO.findAllPictures();
		Iterator<Picture> picturesIterator = pictures.iterator();
		while (picturesIterator.hasNext()) {
			Picture picture = picturesIterator.next();
			picture.setName("event_"+picture.getEvent().getId()+"_0");
			picture.setPath("event_"+picture.getEvent().getId()+"_0");
			pictureService.savePicture(picture);
			System.out.println(picture.getName() + "已经修改！！！");
		}
	}

}
